<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\MeituanSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Model\Meituan;
use App\Application\Admin\Model\Tiktok;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;


#[Middleware(AdminMiddleware::class)]
#[Controller("admin/meituan")]
class MeituanController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {
        $add = 1;
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            //允许开店数量
            $store = Meituan::where('admin_id', $admin_user['admin_user_id'])->count('*');
            if ($store >= $admin_user['subbranch']) {
                $add = 0;
            }

        }

        return [
            'add' => $add,
            'store' => $store,
        ];
    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];

        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['role_id'] > 0) {
            $where[] = ['admin_id', '=', $admin_user['admin_user_id']];
        }
        $store_id = $this->request->input('store_id', '');
        $storelist = Store::where($where)
            ->orderBy('created_at', 'DESC')
            ->pluck('store_name', 'store_id');
        if ($store_id != '') {
            $where[] = ['store_id', 'like', $store_id];
        }
        $lists = Meituan::where($where)
            ->orderBy('store_id', 'DESC')
            ->paginate()->toArray();


        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $storelist[$v['store_id']];

        }
        return compact('lists', 'storelist');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new MeituanSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $meituan_id = $request_param->getMeituanId();
        $client_key = $this->request->input('appkey', '');
        $client_secret = $this->request->input('secret', '');
        $poi_id = $this->request->input('shop_id', '');
        $app_shop_account = $this->request->input('app_shop_account', '');
        $app_shop_accountname = $this->request->input('app_shop_accountname', '');
        if ($this->getadminid() !== 1) {
            $res = Meituan::createMeituan($meituan_id, [
                'appkey' => $client_key,
                'secret' => $client_secret,
                'shop_id' => $poi_id,
                'app_shop_account' => $app_shop_account,
                'app_shop_accountname' => $app_shop_accountname,
                'admin_id' => $this->getadminid(),
                'store_id' => $request_param->getStoreId()
            ]);
        } else {
            $res = Meituan::createMeituan($meituan_id, [
                'appkey' => $client_key,
                'secret' => $client_secret,
                'shop_id' => $poi_id,
                'app_shop_account' => $app_shop_account,
                'app_shop_accountname' => $app_shop_accountname,
                'store_id' => $request_param->getStoreId()
            ]);

        }

        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $meituan_id = (int)$this->request->input('meituan_id', 0);
        return ['title' => $meituan_id > 0 ? '抖音核销配置' : '抖音核销配置'];
    }


    #[Api]
    #[GetMapping("edit/{meituan_id}")]
    function editInfo(int $meituan_id)
    {

        $meituan = Meituan::where('meituan_id', $meituan_id)->select('*')
            ->first() ?: [];
        if ($this->getadminid() !== 1) {
            $storeOptions = Store::where('admin_id', $this->getadminid())->select('*')->get()
                ->toArray();
        } else {
            $storeOptions = Store::where('admin_id', $meituan->admin_id)->select('*')->get()
                ->toArray();
        }

        return compact('meituan', 'storeOptions');
    }


}
